package com.knife.scheduler.config;

import com.knife.scheduler.infra.init.KnifeJobInitListener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.Map;

/**
 * 定时任务创建
 *
 * @author： 76875
 * @date： 2022/4/2 星期六 9:55
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Configuration
public class KnifeJobInitConfigure implements ApplicationListener {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ContextRefreshedEvent){
            ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
            Map<String, KnifeJobInitListener> listener = applicationContext.getBeansOfType(KnifeJobInitListener.class);
            listener.forEach((k,v)->{
                v.contextInitialized(applicationContext);
            });
        }
    }
}
